package org.example.lys.stream;

import org.example.lys.json.JsonUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author liyishan
 * @date 2023/6/21 14:12
 * @apiNote  分组
 */

public class test04 {

    public static void main(String[] args) {
        List<User> userList = build();
        test20002(userList);
    }
    /**
     *  对某个属性分组
     * @param userList
     */
    private static void test1(List<User> userList){
        Map<BigDecimal,List<User>> result= userList.stream().collect(Collectors.groupingBy(User::getPrice));
        System.out.println(JsonUtils.toJson(result));
    }

    private static void test2(List<User> userList){
        Map<BigDecimal,Long> result= userList.stream().collect(Collectors.groupingBy(User::getPrice,Collectors.counting()));
        System.out.println(JsonUtils.toJson(result));
    }

    private static void test20001(List<User> userList){
        // 根据部门和是否退休进行分组，并汇总人数
        Map<String, Map<Integer, Long>> collect5 = userList.stream().collect(Collectors.groupingBy(User::getName, Collectors.groupingBy(User::getIsDelete, Collectors.counting())));
        System.out.println("collect5 = " + collect5);
    }

    private static void test20002(List<User> userList){
        // 根据部门和是否退休进行分组，并取得每组中年龄最大的人
        Map<String, Map<Integer, User>> collect6 = userList.stream().collect(
                Collectors.groupingBy(User::getName,
                        Collectors.groupingBy(User::getIsDelete,
                                Collectors.collectingAndThen(
                                        Collectors.maxBy(
                                                Comparator.comparing(User::getPrice)), Optional::get))));
        System.out.println("collect6 = " + collect6);

    }
    private static void test20003(List<User> userList){
        Map<BigDecimal, IntSummaryStatistics> result= userList.stream().collect(Collectors.groupingBy(User::getPrice,Collectors.summarizingInt(User::getId)));
        System.out.println(JsonUtils.toJson(result));
    }

    private static void test10001(List<User> userList){
        Map<String,List<User>> collect = userList.stream().collect(Collectors.groupingBy(a -> a.getId()+","+ a.getName()));
        System.out.println(JsonUtils.toJson(collect));
    }

    private static void test10002(List<User> userList){
        Map<String,User> map= userList.stream().collect(Collectors.toMap(User::getName, Function.identity(),(v1, v2) -> v1));
        System.out.println(JsonUtils.toJson(map));
    }

    private static void test10003(List<User> userList){
        Map<String,User> map= userList.stream().collect(Collectors.toMap(User::getName, r -> r));
        System.out.println(JsonUtils.toJson(map));
    }



    private static void test10004(List<User> userList){
        Map<String,User> map= userList.stream().collect(Collectors.toMap(key ->key.getPrice().stripTrailingZeros().toPlainString(),value ->value));
        System.out.println(JsonUtils.toJson(map));
    }

    /**
     * 直接获取对象列表的某两个属性为map
     * @param userList
     */
    private static void test111(List<User> userList){
        Map<Integer,String> map = userList.stream().collect(Collectors.toMap(User::getId,User::getName));
        System.out.println(JsonUtils.toJson(map));
    }



    /**
     * 直接获取对象列表的某两个属性为map
     * @param userList
     */
    private static void test11(List<User> userList){
        Map<Integer,String> map = userList.stream().collect(Collectors.toMap(User::getId,User::getName,(a,b) ->a));
        System.out.println(JsonUtils.toJson(map));
    }

    /**
     * 获取对象列表的某两个属性为map  可以修改
     * @param userList
     */
    private static void test12(List<User> userList){
        Map<String, BigDecimal> result = userList.stream().collect(Collectors.toMap(a -> a.getName().toUpperCase(),User::getPrice,(a,b) -> a));
        System.out.println(JsonUtils.toJson(result));
    }



    private static List<User> build(){
        List<User> userList = new ArrayList<>();
        User user1= new User();
        user1.setId(1);
        user1.setName("一");
        user1.setIsDelete(1);
        user1.setPrice(new BigDecimal("1.11"));
        userList.add(user1);
        User user2= new User();
        user2.setId(2);
        user2.setName("三");
        user2.setIsDelete(1);
        user2.setPrice(new BigDecimal("2.22"));
        userList.add(user2);
        User user3= new User();
        user3.setId(3);
        user3.setName("三");
        user3.setIsDelete(0);
        user3.setPrice(new BigDecimal("3.33"));
        userList.add(user3);
        User user31= new User();
        user31.setId(4);
        user31.setName("si");
        user31.setIsDelete(0);
        user31.setPrice(new BigDecimal("3.34"));
        userList.add(user31);
        return userList;
    }

}
